/**
 * Created by zhanglihe on 15/7/7.
 */
/**
 * -------------------------------------------------------
 * 取代alert方法
 * -------------------------------------------------------
 * 使用方法：
 *      Alert({msg: 'xxx'});
 *      Alert({msg: 'xxx', type: 'error'});
 */
var Alert = function (options) {
    if (!options) {
        return false;
    }
    if (typeof options === 'string' || options.nodeType === 1) {
        options = {msg: options, type: 'success'};
    }
    if (typeof(options.type) === 'undefined') {
        options.type = 'success';
    }
    if ($("#kr_alert")) {
        $('#kr_alert').remove();
    }

    $('body').prepend('<div id="kr_alert" class="alert alert_' + options.type + '">' + options.msg + '</div>');
    //位置
    $('#kr_alert').css({left: ((document.documentElement.clientWidth / 2) - ($('#kr_alert').width() / 2)) + 'px'});
    if (typeof(options.time) === 'undefined') {
        options.time = 3;
    }
    setTimeout(function () {
        $('#kr_alert').slideUp(function () {
            $(this).remove();
        });
    }, options.time * 1000);
};
var getUrlPara = function (key) {
    var para = window.location.search;
    if (para) {
        var arr = para.split('?')[1].split('&'),
            len = arr.length,
            obj = {};
        for (var i = 0; i < len; i++) {
            var v = arr[i].split('='),
                k = v[0],
                value = v[1] ? v[1] : '';
            obj[k] = value;
        }
        if (!key) {	//无key值，取全部
            return obj;
        } else {	//有key值，取key值对应值
            return obj[key];
        }
    } else {
        return '';
    }
};

var getUrlPath = function (key) {
    var path = window.location.pathname;
    if (path) {
        var arr = path.split('/');
        if (arr[key]) {
            return arr[key];
        } else {
            return '';
        }
    } else {
        return '';
    }
};

//loading动画
var loading = function () {
    var html = '<div class="spinner" id="KR_LOADING">' +
        '<div class="spinner-container container1">' +
        '<div class="circle1"></div>' +
        '<div class="circle2"></div>' +
        '<div class="circle3"></div>' +
        '<div class="circle4"></div>' +
        '</div>' +
        '<div class="spinner-container container2">' +
        '<div class="circle1"></div>' +
        '<div class="circle2"></div>' +
        '<div class="circle3"></div>' +
        '<div class="circle4"></div>' +
        '</div>' +
        '<div class="spinner-container container3">' +
        '<div class="circle1"></div>' +
        '<div class="circle2"></div>' +
        '<div class="circle3"></div>' +
        '<div class="circle4"></div>' +
        '</div>' +
        '</div>';
    $('body').append(html);
};
var loaded = function () {
    $('#KR_LOADING').remove();
};

//数组删除方法
Array.prototype.remove = function (index) {
    if (typeof this[index] === 'undefined') {
        index = this.indexOf(index);
    }
    if (index > -1) {
        this.splice(index, 1);
    }
};

Array.prototype.indexOf = function (needle) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == needle) {
            return i;
        }
    }
    return -1;
};

//AJAX签名
$(document).ajaxSend(function (evt, request, settings) {
    if (typeof(_global) !== 'undefined' && typeof(_global.access_token) !== 'undefined') {
        request.setRequestHeader('Access-Token', _global.access_token);
    }
});

$(document).ajaxStart(function () {
    loading();
});
$(document).ajaxComplete(function () {
    loaded();
});

$(document).ready(function () {
    $('input[type="text"]').keydown(function (event) {
        switch (event.keyCode) {
            case 13 :
                $('#btn-search').parents('form').submit(function () {
                    return false;
                });
                $('#btn-search').click();
                break;
        }
    });
    /*$(window).scroll(function () {
        if ($('.thead-show').html()) {
            if ($('body').scrollTop() > 100) {
                $('.thead-show').css('position', 'fixed').css('top', '45px').css('width', $('.thead-show').parent().width() + 'px').parent().css('table-layout', 'fixed').find('td,th').attr('width', '150').css('word-wrap', 'break-word');
            } else {
                $('.thead-show').css('position', 'static').css('top', '0px').parent().removeAttr('style').find('td,th').css('word-wrap', 'normal').removeAttr('width');
            }
        }
    });*/
});